home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 22 / AACD 22.iso / AACD / Programming / Emperor_v4.0 / Emperor / Projects / Resistor.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-05  |  15.9 KB  |  444 lines

  1. void Calculate(void);
  2. void AllocListbrowserNodesExtra(struct List *, char labels[96][5], WORD);
  3. char *shift_comma_in_string(char *, const char *, UBYTE);
  4. STRPTR floattostring(DOUBLE, UBYTE);
  5.  
  6. char string[5][96][5];
  7. struct IntuiText WindowIntuitext;
  8. struct List Listbrowser_List_E6;
  9. struct List Listbrowser_List_E12;
  10. struct List Listbrowser_List_E24;
  11. struct List Listbrowser_List_E48;
  12. struct List Listbrowser_List_E96;
  13. struct RastPort *WindowRastport;
  14.  
  15. void Startup(void)
  16. {
  17.     /** This function calculates the values    **/
  18.     /** later shown within the listbrowser.    **/
  19.     /** Each value is calculated by extracting **/
  20.     /** the m-th root of 10, a countervalue n  **/
  21.     /** while 0 <= n < m, that powers the root **/
  22.     /** and m is the nominalvalue 6 * 2^(0..4) **/
  23.     /** Also here are the single functions for **/
  24.     /** getting the colours to draw the layout **/
  25.  
  26.     DOUBLE i;
  27.  
  28.     NewList(&Listbrowser_List_E6);
  29.     NewList(&Listbrowser_List_E12);
  30.     NewList(&Listbrowser_List_E24);
  31.     NewList(&Listbrowser_List_E48);
  32.     NewList(&Listbrowser_List_E96);
  33.     for(i = 0;i <  6;i++)
  34.     {
  35.       strcpy(string[0][(ULONG) i], floattostring(pow(10, i / 6), 1));
  36.       strcat(string[0][(ULONG) i], "0");
  37.     }
  38.     for(i = 0;i < 12;i++)
  39.     {
  40.       strcpy(string[1][(ULONG) i], floattostring(pow(10, i / 12), 1));
  41.       strcat(string[1][(ULONG) i], "0");
  42.     }
  43.     for(i = 0;i < 24;i++)
  44.     {
  45.       strcpy(string[2][(ULONG) i], floattostring(pow(10, i / 24), 1));
  46.       strcat(string[2][(ULONG) i], "0");
  47.     }
  48.     for(i = 0;i < 48;i++) strcpy(string[3][(ULONG) i], floattostring(pow(10, i / 48), 2));
  49.     for(i = 0;i < 96;i++) strcpy(string[4][(ULONG) i], floattostring(pow(10, i / 96), 2));
  50.     strcpy(string[0][3],  "3.30");
  51.     strcpy(string[0][4],  "4.70");
  52.     strcpy(string[1][5],  "2.70");
  53.     strcpy(string[1][6],  "3.30");
  54.     strcpy(string[1][7],  "3.90");
  55.     strcpy(string[1][8],  "4.70");
  56.     strcpy(string[1][11], "8.20");
  57.     strcpy(string[2][10], "2.70");
  58.     strcpy(string[2][11], "3.00");
  59.     strcpy(string[2][12], "3.30");
  60.     strcpy(string[2][13], "3.60");
  61.     strcpy(string[2][14], "3.90");
  62.     strcpy(string[2][15], "4.30");
  63.     strcpy(string[2][16], "4.70");
  64.     strcpy(string[2][22], "8.20");
  65.     AllocListbrowserNodesExtra(&Listbrowser_List_E6,  string[0],  6);
  66.     AllocListbrowserNodesExtra(&Listbrowser_List_E12, string[1], 12);
  67.     AllocListbrowserNodesExtra(&Listbrowser_List_E24, string[2], 24);
  68.     AllocListbrowserNodesExtra(&Listbrowser_List_E48, string[3], 48);
  69.     AllocListbrowserNodesExtra(&Listbrowser_List_E96, string[4], 96);
  70.     sw = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x00000000, 0x00000000, 0x00000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  71.     br = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x96000000, 0x3C000000, 0x00000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  72.     rt = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xFFFFFFFF, 0x00000000, 0x00000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  73.     or = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xFFFFFFFF, 0x96000000, 0x32000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  74.     ge = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  75.     gn = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x00000000, 0xFFFFFFFF, 0x00000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  76.     bl = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x00000000, 0x00000000, 0xFFFFFFFF, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  77.     vl = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  78.     gr = ObtainBestPen(Screen1->ViewPort.ColorMap, 0x64000000, 0x64000000, 0x64000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  79.     ws = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  80.     au = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xE6000000, 0xE6000000, 0x64000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  81.     ag = ObtainBestPen(Screen1->ViewPort.ColorMap, 0xD2000000, 0xD2000000, 0xBE000000, OBP_Precision, PRECISION_IMAGE, TAG_DONE);
  82. }
  83.  
  84. void Shutdown(void)
  85. {
  86.     /** Release all pens and free the lists when done **/
  87.  
  88.     ReleasePen(Screen1->ViewPort.ColorMap, sw);
  89.     ReleasePen(Screen1->ViewPort.ColorMap, br);
  90.     ReleasePen(Screen1->ViewPort.ColorMap, rt);
  91.     ReleasePen(Screen1->ViewPort.ColorMap, or);
  92.     ReleasePen(Screen1->ViewPort.ColorMap, ge);
  93.     ReleasePen(Screen1->ViewPort.ColorMap, gn);
  94.     ReleasePen(Screen1->ViewPort.ColorMap, bl);
  95.     ReleasePen(Screen1->ViewPort.ColorMap, vl);
  96.     ReleasePen(Screen1->ViewPort.ColorMap, gr);
  97.     ReleasePen(Screen1->ViewPort.ColorMap, ws);
  98.     ReleasePen(Screen1->ViewPort.ColorMap, au);
  99.     ReleasePen(Screen1->ViewPort.ColorMap, ag);
  100.     FreeListBrowserList(&Listbrowser_List_E6);
  101.     FreeListBrowserList(&Listbrowser_List_E12);
  102.     FreeListBrowserList(&Listbrowser_List_E24);
  103.     FreeListBrowserList(&Listbrowser_List_E48);
  104.     FreeListBrowserList(&Listbrowser_List_E96);
  105. }
  106.  
  107. void Chooser1_GadgetUp_Event(void)
  108. {
  109.     /** Function copies listbrowser-entrys into the    **/
  110.     /** listbrowser (see the typecasted argument:      **/
  111.     /** "(STRPTR) &Listbrowser_List_EXX" for the list) **/
  112.  
  113.     switch(stringtoint(Emperor_GetGadgetAttr(Chooser1)))
  114.     {
  115.         case 0: Emperor_SetGadgetAttrComplex(Listbrowser1, LISTBROWSER_Labels, (STRPTR) &Listbrowser_List_E6);  break;
  116.         case 1: Emperor_SetGadgetAttrComplex(Listbrowser1, LISTBROWSER_Labels, (STRPTR) &Listbrowser_List_E12); break;
  117.         case 2: Emperor_SetGadgetAttrComplex(Listbrowser1, LISTBROWSER_Labels, (STRPTR) &Listbrowser_List_E24); break;
  118.         case 3: Emperor_SetGadgetAttrComplex(Listbrowser1, LISTBROWSER_Labels, (STRPTR) &Listbrowser_List_E48); break;
  119.         case 4: Emperor_SetGadgetAttrComplex(Listbrowser1, LISTBROWSER_Labels, (STRPTR) &Listbrowser_List_E96); break;
  120.     }
  121.     Calculate();
  122. }
  123.  
  124. void Chooser2_GadgetUp_Event(void)
  125. {
  126.     Calculate();
  127. }
  128.  
  129. void Listbrowser1_GadgetUp_Event(void)
  130. {
  131.     Calculate();
  132. }
  133.  
  134. void Window1_ShowWindow_Event(void)
  135. {
  136.     Emperor_SetGadgetAttrComplex(Listbrowser1, LISTBROWSER_Labels, (STRPTR) &Listbrowser_List_E12);
  137.     Emperor_SetGadgetAttr(Listbrowser1, "0");
  138.     Emperor_SetGadgetAttr(Chooser1, "1");
  139.     Emperor_SetGadgetAttr(Chooser2, "5");
  140.     WindowRastport = Window1->RPort;
  141.     Calculate();
  142. }
  143.  
  144. void Window1_CloseWindow_Event(void)
  145. {
  146.     if(Quitrequest()) Emperor_QuitProgram();
  147. }
  148.  
  149. void Menu_Quit_MenuPick_Event(void)
  150. {
  151.     if(Quitrequest()) Emperor_QuitProgram();
  152. }
  153.  
  154. void Menu_Information_MenuPick_Event(void)
  155. {
  156.     Inforequest();
  157. }
  158.  
  159. void Menu_E6_MenuPick_Event(void)
  160. {
  161.     Emperor_SetGadgetAttr(Chooser1, "0");
  162.     Chooser1_GadgetUp_Event();
  163.     Calculate();
  164. }
  165.  
  166. void Menu_E12_MenuPick_Event(void)
  167. {
  168.     Emperor_SetGadgetAttr(Chooser1, "1");
  169.     Chooser1_GadgetUp_Event();
  170.     Calculate();
  171. }
  172.  
  173. void Menu_E24_MenuPick_Event(void)
  174. {
  175.     Emperor_SetGadgetAttr(Chooser1, "2");
  176.     Chooser1_GadgetUp_Event();
  177.     Calculate();
  178. }
  179.  
  180. void Menu_E48_MenuPick_Event(void)
  181. {
  182.     Emperor_SetGadgetAttr(Chooser1, "3");
  183.     Chooser1_GadgetUp_Event();
  184.     Calculate();
  185. }
  186.  
  187. void Menu_E96_MenuPick_Event(void)
  188. {
  189.     Emperor_SetGadgetAttr(Chooser1, "4");
  190.     Chooser1_GadgetUp_Event();
  191.     Calculate();
  192. }
  193.  
  194. void Calculate(void)
  195. {
  196.     /** This is the main-calculation function, which   **/
  197.     /** calculates the colours of the resistorlayout   **/
  198.     /** and shows its results within the layout-gadget **/
  199.  
  200.     BYTE counter;
  201.     BYTE nextring;
  202.     BYTE tolerance;
  203.     BYTE multiplicator;
  204.     BYTE resistorentry;
  205.     char *resistorvalue = "1234567890";
  206.  
  207.     tolerance = stringtoint(Emperor_GetGadgetAttr(Chooser1));
  208.     multiplicator = stringtoint(Emperor_GetGadgetAttr(Chooser2));
  209.     resistorentry = stringtoint(Emperor_GetGadgetAttr(Listbrowser1));
  210.  
  211.     SetAPen(WindowRastport, 0);
  212.     RectFill(WindowRastport, 210, 50, 390, 310);
  213.  
  214.     SetAPen(WindowRastport, 2);
  215.     Move(WindowRastport, 366, 139);
  216.     Draw(WindowRastport, 234, 139);
  217.     Draw(WindowRastport, 234, 191);
  218.  
  219.     SetAPen(WindowRastport, 1);
  220.     Move(WindowRastport, 234, 191);
  221.     Draw(WindowRastport, 366, 191);
  222.     Draw(WindowRastport, 366, 139);
  223.  
  224.     Move(WindowRastport, 367, 138);
  225.     Draw(WindowRastport, 233, 138);
  226.     Draw(WindowRastport, 233, 192);
  227.     Draw(WindowRastport, 367, 192);
  228.     Draw(WindowRastport, 367, 138);
  229.  
  230.     Move(WindowRastport, 233, 163);
  231.     Draw(WindowRastport, 222, 163);
  232.     Draw(WindowRastport, 222, 167);
  233.     Draw(WindowRastport, 233, 167);
  234.     Draw(WindowRastport, 233, 163);
  235.  
  236.     Move(WindowRastport, 367, 163);
  237.     Draw(WindowRastport, 378, 163);
  238.     Draw(WindowRastport, 378, 167);
  239.     Draw(WindowRastport, 367, 167);
  240.     Draw(WindowRastport, 367, 163);
  241.  
  242.     Move(WindowRastport, 240, 140);
  243.     Draw(WindowRastport, 255, 140);
  244.     Draw(WindowRastport, 255, 190);
  245.     Draw(WindowRastport, 240, 190);
  246.     Draw(WindowRastport, 240, 140);
  247.  
  248.     Move(WindowRastport, 260, 140);
  249.     Draw(WindowRastport, 275, 140);
  250.     Draw(WindowRastport, 275, 190);
  251.     Draw(WindowRastport, 260, 190);
  252.     Draw(WindowRastport, 260, 140);
  253.  
  254.     Move(WindowRastport, 280, 140);
  255.     Draw(WindowRastport, 295, 140);
  256.     Draw(WindowRastport, 295, 190);
  257.     Draw(WindowRastport, 280, 190);
  258.     Draw(WindowRastport, 280, 140);
  259.  
  260.     if(tolerance >= 1)
  261.     {
  262.         Move(WindowRastport, 300, 140);
  263.         Draw(WindowRastport, 315, 140);
  264.         Draw(WindowRastport, 315, 190);
  265.         Draw(WindowRastport, 300, 190);
  266.         Draw(WindowRastport, 300, 140);
  267.     }
  268.  
  269.     if(tolerance >= 3)
  270.     {
  271.         Move(WindowRastport, 320, 140);
  272.         Draw(WindowRastport, 335, 140);
  273.         Draw(WindowRastport, 335, 190);
  274.         Draw(WindowRastport, 320, 190);
  275.         Draw(WindowRastport, 320, 140);
  276.     }
  277.  
  278.     switch(multiplicator)
  279.     {
  280.         case 0:  shift_comma_in_string(resistorvalue, string[tolerance][resistorentry], 1); break;
  281.         case 1:  shift_comma_in_string(resistorvalue, string[tolerance][resistorentry], 2); break;
  282.         case 3:  shift_comma_in_string(resistorvalue, string[tolerance][resistorentry], 1); break;
  283.         case 4:  shift_comma_in_string(resistorvalue, string[tolerance][resistorentry], 2); break;
  284.         case 6:  shift_comma_in_string(resistorvalue, string[tolerance][resistorentry], 1); break;
  285.         case 7:  shift_comma_in_string(resistorvalue, string[tolerance][resistorentry], 2); break;
  286.         case 9:  shift_comma_in_string(resistorvalue, string[tolerance][resistorentry], 1); break;
  287.         case 10: shift_comma_in_string(resistorvalue, string[tolerance][resistorentry], 2); break;
  288.         default: strcpy(resistorvalue, string[tolerance][resistorentry]);
  289.     }
  290.     if(multiplicator <= 1) strcat(resistorvalue, " mOhm");
  291.     else
  292.     {
  293.         if(multiplicator <= 4) strcat(resistorvalue, " Ohm");
  294.         else
  295.         {
  296.             if(multiplicator <= 7) strcat(resistorvalue, " kOhm");
  297.             else
  298.             {
  299.                 if(multiplicator <= 10) strcat(resistorvalue, " MOhm");
  300.                 else strcat(resistorvalue, " GOhm");
  301.             }
  302.         }
  303.     }
  304.     WindowIntuitext.FrontPen = 1;
  305.     WindowIntuitext.LeftEdge = 250;
  306.     WindowIntuitext.TopEdge = 100;
  307.     WindowIntuitext.IText = (UBYTE *) resistorvalue;
  308.     PrintIText(WindowRastport, &WindowIntuitext, 0, 0);
  309.  
  310.     switch(multiplicator)
  311.     {
  312.         case 0:  SetAPen(WindowRastport, ag); break;
  313.         case 1:  SetAPen(WindowRastport, au); break;
  314.         case 2:  SetAPen(WindowRastport, sw); break;
  315.         case 3:  SetAPen(WindowRastport, br); break;
  316.         case 4:  SetAPen(WindowRastport, rt); break;
  317.         case 5:  SetAPen(WindowRastport, or); break;
  318.         case 6:  SetAPen(WindowRastport, ge); break;
  319.         case 7:  SetAPen(WindowRastport, gn); break;
  320.         case 8:  SetAPen(WindowRastport, bl); break;
  321.         case 9:  SetAPen(WindowRastport, vl); break;
  322.         case 10: SetAPen(WindowRastport, gr); break;
  323.         case 11: SetAPen(WindowRastport, ws); break;
  324.     }
  325.     if(tolerance >= 3) RectFill(WindowRastport, 301, 141, 314, 189);
  326.     else RectFill(WindowRastport, 281, 141, 294, 189);
  327.     switch(tolerance)
  328.     {
  329.         case 1:  SetAPen(WindowRastport, ag); break;
  330.         case 2:  SetAPen(WindowRastport, au); break;
  331.         case 3:  SetAPen(WindowRastport, rt); break;
  332.         case 4:  SetAPen(WindowRastport, br); break;
  333.         default: SetAPen(WindowRastport,  0); break;
  334.     }
  335.     if(tolerance >= 3) RectFill(WindowRastport, 321, 141, 334, 189);
  336.     else RectFill(WindowRastport, 301, 141, 314, 189);
  337.     for(counter = 0, nextring = 0;counter <= 3;counter++)
  338.     {
  339.         if(counter == 1) continue;
  340.         if((tolerance < 3) && (counter == 3)) break;
  341.         switch(string[tolerance][resistorentry][counter])
  342.         {
  343.             case '0': SetAPen(WindowRastport, sw); break;
  344.             case '1': SetAPen(WindowRastport, br); break;
  345.             case '2': SetAPen(WindowRastport, rt); break;
  346.             case '3': SetAPen(WindowRastport, or); break;
  347.             case '4': SetAPen(WindowRastport, ge); break;
  348.             case '5': SetAPen(WindowRastport, gn); break;
  349.             case '6': SetAPen(WindowRastport, bl); break;
  350.             case '7': SetAPen(WindowRastport, vl); break;
  351.             case '8': SetAPen(WindowRastport, gr); break;
  352.             case '9': SetAPen(WindowRastport, ws); break;
  353.         }
  354.         RectFill(WindowRastport, 241 + 20 * nextring, 141, 254 + 20 * nextring, 189);
  355.         nextring++;
  356.     }
  357. }
  358.  
  359. /** Now some low-level funcs.. **/
  360.  
  361. void AllocListbrowserNodesExtra(struct List *list, char labels[96][5], WORD number_of_entrys)
  362. {
  363.     /** Extra-function for creating lists, that **/
  364.     /** will be shown within the listbrowser    **/
  365.  
  366.     WORD counter;
  367.     struct Node *node;
  368.  
  369.     NewList(list);
  370.     for(counter = 0;counter < number_of_entrys;counter++)
  371.     {
  372.         node = AllocListBrowserNode(1, LBNCA_Text, labels[counter], LBNCA_CopyText, TRUE, TAG_DONE);
  373.         AddTail(list, node);
  374.     }
  375. }
  376.  
  377. STRPTR floattostring(DOUBLE number_to_convert, UBYTE digits_behind_comma)
  378. {
  379.     /** This function converts a double    **/
  380.     /** precision floatvalue into a string **/
  381.  
  382.     ULONG integer_number;
  383.     UWORD integer_length;
  384.     UWORD single_digit;
  385.     char *buffstr = "1234567890";
  386.  
  387.     strcpy(buffstr, "");
  388.     if(number_to_convert < 0)
  389.     {
  390.         strcpy(buffstr, "-");
  391.         number_to_convert = -number_to_convert;
  392.     }
  393.     integer_number = (ULONG) ((number_to_convert * pow(10, digits_behind_comma)) + 0.5);
  394.     if(integer_number > 0) integer_length = 1;
  395.     if(integer_number/10 > 0) integer_length = 2;
  396.     if(integer_number/100 > 0) integer_length = 3;
  397.     if(integer_number/1000 > 0) integer_length = 4;
  398.     if(integer_number/10000 > 0) integer_length = 5;
  399.     if(integer_number/100000 > 0) integer_length = 6;
  400.     if(integer_number/1000000 > 0) integer_length = 7;
  401.     if(integer_number/10000000 > 0) integer_length = 8;
  402.     if(integer_number/100000000 > 0) integer_length = 9;
  403.     if(integer_number == 0)  strcpy(buffstr, "0");
  404.     else
  405.     {
  406.         for(integer_length = integer_length;integer_length > 0;integer_length--)
  407.         {
  408.             single_digit = (UWORD) (integer_number / pow(10, integer_length - 1));
  409.             if(single_digit == 0) strcat(buffstr, "0");
  410.             if(single_digit == 1) strcat(buffstr, "1");
  411.             if(single_digit == 2) strcat(buffstr, "2");
  412.             if(single_digit == 3) strcat(buffstr, "3");
  413.             if(single_digit == 4) strcat(buffstr, "4");
  414.             if(single_digit == 5) strcat(buffstr, "5");
  415.             if(single_digit == 6) strcat(buffstr, "6");
  416.             if(single_digit == 7) strcat(buffstr, "7");
  417.             if(single_digit == 8) strcat(buffstr, "8");
  418.             if(single_digit == 9) strcat(buffstr, "9");
  419.             integer_number -= single_digit * pow(10, integer_length - 1);
  420.             if(integer_length == digits_behind_comma + 1) strcat(buffstr, ".");
  421.         }
  422.     }
  423.     return(buffstr);
  424. }
  425.  
  426. char *shift_comma_in_string(char *buffer1, const char *buffer2, UBYTE number_of_bytes)
  427. {
  428.     BOOL comma_found = FALSE;
  429.     char *d = buffer1, c;
  430.  
  431.     do
  432.     {
  433.         c = *buffer2++;
  434.         if(c != '.') *buffer1++ = c;
  435.         else comma_found = TRUE;
  436.         if(!number_of_bytes) *buffer1++ = '.';
  437.         if(comma_found) number_of_bytes--;
  438.     }
  439.     while(c);
  440.  
  441.     return(d);
  442. }
  443.  
  444.